package com.tz.system.menu.service.impl;

import java.util.List;
import java.util.Map;

import javax.transaction.Transactional;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tz.platform.common.CacheConstans;
import com.tz.platform.page.PageCondition;
import com.tz.platform.utils.ReturnUtil;
import com.tz.platform.utils.StringUtil;
import com.tz.system.cache.service.ParameterCache;
import com.tz.system.menu.dao.TSysMenuDao;
import com.tz.system.menu.model.TSysMenu;
import com.tz.system.menu.service.TSysMenuService;


@Service("tSysMenuServiceImpl")
public  class TSysMenuServiceImpl implements TSysMenuService {

	@Autowired
	private ParameterCache parameterCache;   //加载缓存
	
	@Autowired
	private TSysMenuDao tSysMenuDao;
	
	//查询分页
	private static String queryPageTSysMenuHql = " from TSysMenu ";
	
	public static final String menuHql = " from TSysMenu where menuCode=? ";
	
	//根绝角色code获取角色对应的菜单列表
	private static String querySysMenuByRoleCode = " from TSysMenu where menuCode in (select menuCode from TMgtRolePrivate where roleCode=? ) ";
	
	//获取最大编码
	private static String getMaxMenuCodeHql = "select max(menuCode) from TSysMenu where upMenuCode= ";

	@Override
	@Transactional
	public PageCondition findByPage(PageCondition condition) {
		condition = tSysMenuDao.findByPage(queryPageTSysMenuHql, condition);
		return condition;
	}
	
	@Override
	@Transactional
	public Map<String, Object> removeById(Integer id) {
		try {
			TSysMenu tSysMenu = tSysMenuDao.get(id);
			if(tSysMenu!=null){
				String menuCode =  tSysMenu.getMenuCode();
				//删除菜单对应的角色信息
				if(!StringUtils.isEmpty(menuCode)){
					tSysMenuDao.executeByHql("delete from TMgtRolePrivate where menuCode=?", menuCode);
				}
				//删除菜单信息
				tSysMenuDao.removeById(id);
				//删除redis
				parameterCache.deleteRedis(CacheConstans.SYSMENU,menuCode);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ReturnUtil.returnMessage("fail", "删除失败！");
		}
		return ReturnUtil.returnMessage("success", "删除成功！");
	}
	
	@Override
	@Transactional
	public Map<String, Object> addSave(TSysMenu entity) {
		// 1、检查菜单编号id是否重复
		TSysMenu tmenu = null;
		if (null != entity && !StringUtils.isEmpty(entity.getMenuCode())) {
			tmenu = this.queryByMenuCode(entity.getMenuCode());
		}
		if (tmenu != null) {
			return ReturnUtil.returnMessage("fail", "编号重复");
		}
		try {
			setMenuInfo(entity);
			tSysMenuDao.save(entity);
			if(entity.getId()!=null){
				parameterCache.loadMenuInfo(entity.getMenuCode());
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ReturnUtil.returnMessage("fail", "添加失败！");
		}
		return ReturnUtil.returnMessage("success", "添加成功！");
	}
	
	@Override
	@Transactional
	public Map<String, Object> updateSave(TSysMenu entity) {
		try {
			setMenuInfo(entity);
			tSysMenuDao.saveOrUpdate(entity);
			//加载redis缓存
			parameterCache.loadMenuInfo(entity.getMenuCode());
		} catch (Exception e) {
			e.printStackTrace();
			return ReturnUtil.returnMessage("fail", "修改失败！");
		}
		return ReturnUtil.returnMessage("success", "修改成功！");
	}
	
	@Override
	@Transactional
	public TSysMenu queryById(Integer id) {
		return tSysMenuDao.get(id);
	}
	
	@Override
	@Transactional
	public TSysMenu queryByMenuCode(String menuCode) {
		return tSysMenuDao.findUnique(menuHql, menuCode);
	}
	
	@Override
	@Transactional
	public List<TSysMenu> listAllMenus() {
		return tSysMenuDao.getAll(); 
	}
	
	
	/**
	 * 
	 * getMenu:(设置菜单的信息). 
	 *
	 * @author changhz
	 * @param entity
	 * @return
	 */
	@Transactional
	public void setMenuInfo(TSysMenu entity){
		//判断上级菜单是否为空
		if(StringUtil.msIsEmpty(entity.getMenuCode()) || 
				(!StringUtils.isEmpty(entity.getMenuCode()) && 
				 !StringUtils.isEmpty(entity.getUpMenuCode()) &&
				 !entity.getMenuCode().startsWith(entity.getUpMenuCode()))){
			entity.setMenuCode(generatateMenuCode(entity));
		}
		
		if(StringUtil.msIsEmpty(entity.getUpMenuCode())){
			entity.setUpMenuCode("0");
			entity.setMenuLevel(1);
		}else{
			entity.setMenuLevel(2);
		}
		entity.setSort(entity.getMenuCode());
	}
	
	/**
	 * 自动生成菜单的编号
	 * @return
	 */
	public String generatateMenuCode(TSysMenu entity){
		String sql ="";
		if(StringUtils.isEmpty(entity.getUpMenuCode())){
			sql= getMaxMenuCodeHql + "0";
		}else{
			sql= getMaxMenuCodeHql + entity.getUpMenuCode();
		}
		long result= 0;
		String upMenuCode=tSysMenuDao.getMaxValue(sql);
		if(!StringUtils.isEmpty(upMenuCode)){
			result=Long.valueOf(upMenuCode);
		}else{
			result=Long.valueOf(entity.getUpMenuCode()+"00");
		}
		return String.valueOf(result + 1);
	}


	@Override
	public List<TSysMenu> getMenusByRoleCode(String roleCode) {
		List<TSysMenu> list = tSysMenuDao.find(querySysMenuByRoleCode, roleCode);
		return list;
	}


}

